package com.oracle.oa.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.MessageBiz;
import com.oracle.oa.biz.MessageToUserBiz;
import com.oracle.oa.biz.ReadCommonMessageBiz;
import com.oracle.oa.biz.UserInfoBiz;
import com.oracle.oa.entity.Message;
import com.oracle.oa.entity.Messagetouser;
import com.oracle.oa.entity.Userinfo;

public class TimeSelectAction extends ActionSupport implements SessionAware {

	private String btime;
	private String etime;
	private String search;
	private String page;
	
	Map<String, Object> session;
	
	UserInfoBiz ubiz;
	MessageBiz mbiz;
	MessageToUserBiz mtubiz;
	ReadCommonMessageBiz rcmbiz;
	
	public String searchbytime(){
		List<Message> messagelist = new ArrayList();
		Userinfo userinfo= (Userinfo) session.get("userinfo");
		if(btime!=null && etime!=null && btime.length()>0 && etime.length()>0){
			String time1="";
			String time2="";
			time1=btime+" 00:00:00";
			time2=etime+" 23:59:59";
			//判断登录者权限
			if(userinfo.getRoleinfo().getRoleid()==1){
				//超级管理员
				messagelist=mbiz.getByRecordtime(time1, time2);
			}else if(userinfo.getRoleinfo().getRoleid()==2){
				//部门管理员
				System.out.println(userinfo.getDepartid());
				List<Userinfo> list=ubiz.getByProperty(Userinfo.class, "departid", userinfo.getDepartid());
				if(list!=null && list.size()>0){
					for(int i=0;i<list.size();i++){
						Userinfo u = list.get(i);//得到员工id
						//得到每个员工发送的消息集合
						List<Message> mmlist = mbiz.getByRecordtimeAndUser(u, time1, time2);		
						//添加到messagelist集合中
						if(mmlist!=null && mmlist.size()>0){
							for(int j=0;j<mmlist.size();j++){
								Message ml = mmlist.get(j);
								messagelist.add(ml);
							}
						}
					}
				}
				
			}else if(userinfo.getRoleinfo().getRoleid()==3){
				//普通员工
				messagelist=mbiz.getByRecordtimeAndUser(userinfo, time1, time2);
			}
			session.put("messagelist", messagelist);
		}else if(search!=null){
			SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat smonth = new SimpleDateFormat("yyyy-MM");
			String time1="";
			String time2="";
			String s="";
			if("day".equals(search)){
				s=sm.format(new Date());
				time1=s+" 00:00:00";
				time2=s+" 23:59:59";
				//判断登录者权限
				if(userinfo.getRoleinfo().getRoleid()==1){
					//超级管理员
					messagelist=mbiz.getByRecordtime(time1, time2);
				}else if(userinfo.getRoleinfo().getRoleid()==2){
					//部门管理员
					List<Userinfo> list=ubiz.getByProperty(Userinfo.class, "departid", userinfo.getDepartid());
					if(list!=null && list.size()>0){
						for(int i=0;i<list.size();i++){
							Userinfo u = list.get(i);//得到员工id
							//得到每个员工发送的消息集合
							List<Message> mmlist = mbiz.getByRecordtimeAndUser(u, time1, time2);		
							//添加到messagelist集合中
							if(mmlist!=null && mmlist.size()>0){
								for(int j=0;j<mmlist.size();j++){
									Message ml = mmlist.get(j);
									messagelist.add(ml);
								}
							}
						}
					}
				}else if(userinfo.getRoleinfo().getRoleid()==3){
					//普通员工
					messagelist=mbiz.getByRecordtimeAndUser(userinfo, time1, time2);
				}
				session.put("messagelist", messagelist);
			}else if("week".equals(search)){
				Calendar c = Calendar.getInstance();
				int dayofweek = c.get(Calendar.DAY_OF_WEEK);
				if(dayofweek==1){
					c.add(Calendar.DATE, -7);
				}else{
					c.add(Calendar.DATE, -(dayofweek-2));
				}			
				Date date = c.getTime();
				time1=sm.format(date)+" 00:00:00";
				time2=sm.format(new Date())+" 23:59:59";		
				//判断登录者权限
				if(userinfo.getRoleinfo().getRoleid()==1){
					//超级管理员
					messagelist=mbiz.getByRecordtime(time1, time2);
				}else if(userinfo.getRoleinfo().getRoleid()==2){
					//部门管理员
					List<Userinfo> list=ubiz.getByProperty(Userinfo.class, "departid", userinfo.getDepartid());
					if(list!=null && list.size()>0){
						for(int i=0;i<list.size();i++){
							Userinfo u = list.get(i);//得到员工id
							//得到每个员工发送的消息集合
							List<Message> mmlist = mbiz.getByRecordtimeAndUser(u, time1, time2);		
							//添加到messagelist集合中
							if(mmlist!=null && mmlist.size()>0){
								for(int j=0;j<mmlist.size();j++){
									Message ml = mmlist.get(j);
									messagelist.add(ml);
								}
							}
						}
					}
				}else if(userinfo.getRoleinfo().getRoleid()==3){
					//普通员工
					messagelist=mbiz.getByRecordtimeAndUser(userinfo, time1, time2);
				}
				session.put("messagelist", messagelist);
			}else if("month".equals(search)){
				s=smonth.format(new Date());
				time1=s+"-01 00:00:00";
				time2=s+"-31 23:59:59";
				//判断登录者权限
				if(userinfo.getRoleinfo().getRoleid()==1){
					//超级管理员
					messagelist=mbiz.getByRecordtime(time1, time2);
				}else if(userinfo.getRoleinfo().getRoleid()==2){
					//部门管理员
					List<Userinfo> list=ubiz.getByProperty(Userinfo.class, "departid", userinfo.getDepartid());
					if(list!=null && list.size()>0){
						for(int i=0;i<list.size();i++){
							Userinfo u = list.get(i);//得到员工id
							//得到每个员工发送的消息集合
							List<Message> mmlist = mbiz.getByRecordtimeAndUser(u, time1, time2);		
							//添加到messagelist集合中
							if(mmlist!=null && mmlist.size()>0){
								for(int j=0;j<mmlist.size();j++){
									Message ml = mmlist.get(j);
									messagelist.add(ml);
								}
							}
						}
					}
				}else if(userinfo.getRoleinfo().getRoleid()==3){
					//普通员工
					messagelist=mbiz.getByRecordtimeAndUser(userinfo, time1, time2);
				}
				session.put("messagelist", messagelist);
			}
		}else {
			//判断登录者权限
			if(userinfo.getRoleinfo().getRoleid()==1){
				//超级管理员
				messagelist=mbiz.getAll(Message.class);
			}else if(userinfo.getRoleinfo().getRoleid()==2){
				//部门管理员
				//查询本部门的员工
				List<Userinfo> list=ubiz.getByProperty(Userinfo.class, "departid", userinfo.getDepartid());
				if(list!=null && list.size()>0){
					for(int i=0;i<list.size();i++){
						Userinfo u = list.get(i);//得到员工id
						//得到每个员工发送的消息集合
						List<Message> mmlist = mbiz.getByProperty(Message.class, "userinfo.userid", u.getUserid());
						//添加到messagelist集合中
						if(mmlist!=null && mmlist.size()>0){
							for(int j=0;j<mmlist.size();j++){
								
								Message ml = mmlist.get(j);
								messagelist.add(ml);
							}
						}
					}
				}
			}else if(userinfo.getRoleinfo().getRoleid()==3){
				//普通员工
				messagelist=mbiz.getByProperty(Message.class, "userinfo.userid", userinfo.getUserid());
			}
		}
		
		
		if(page!=null){
			session.put("page", page);
		}
		int type=1;
		String up="no";
		List<Messagetouser> messagetouserList=mtubiz.getAll(Messagetouser.class);
		session.put("type", type);
		session.put("up", up);
		session.put("messagetouserList", messagetouserList);
		session.put("messagelist", messagelist);
		return SUCCESS;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}
	
	
	public String getBtime() {
		return btime;
	}

	public void setBtime(String btime) {
		this.btime = btime;
	}

	public String getEtime() {
		return etime;
	}

	public void setEtime(String etime) {
		this.etime = etime;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public MessageBiz getMbiz() {
		return mbiz;
	}

	public void setMbiz(MessageBiz mbiz) {
		this.mbiz = mbiz;
	}

	public MessageToUserBiz getMtubiz() {
		return mtubiz;
	}

	public void setMtubiz(MessageToUserBiz mtubiz) {
		this.mtubiz = mtubiz;
	}

	public ReadCommonMessageBiz getRcmbiz() {
		return rcmbiz;
	}

	public void setRcmbiz(ReadCommonMessageBiz rcmbiz) {
		this.rcmbiz = rcmbiz;
	}

	public UserInfoBiz getUbiz() {
		return ubiz;
	}

	public void setUbiz(UserInfoBiz ubiz) {
		this.ubiz = ubiz;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}
	
	
}
